#include "processbar.h"

const char circle[] = "\\|/-";
char bar[NUM];

void processbar(int rate)
{
    if (rate < 0 || rate > 100) return;
    int len = strlen(circle);
    // printf("[%s%-100s%s][%d%%][%c]\r", BLUE2, bar, NONE, rate, circle[rate % len]); // 打印色块
    printf("[");
    printf(BLUE2"%s"NONE, bar);
    if (rate < 100) printf("%*s", 100 - rate, " ");
    printf("][%d%%][%c]\r", rate, circle[rate % len]);
    fflush(stdout);
    bar[rate++] = STYLE3;
}

void barInit()
{
    memset(bar, '\0', sizeof(bar));
}

/* v2
void processbar(int rate)
{
    if (rate < 0 || rate > 100) return;
    int len = strlen(circle);
    printf("%s[%-100s][%d%%][%c]%s\r", BLUE, bar, rate, circle[rate % len], NONE);
    fflush(stdout);
    bar[rate++] = STYLE2;
    if (rate < 100) bar[rate] = RIGHT;
}

void barInit()
{
    memset(bar, '\0', sizeof(bar));
}
*/


/*  v1
void processbar()
{
    int count = 0;
    char bar[NUM] = { '\0' };
    int len = strlen(circle);
    while (count <= LIMIT)
    {
        printf("[%-100s][%d%%][%c]\r", bar, count, circle[count % len]);
        fflush(stdout);
        bar[count++] = STYLE2;
        if (count < 100) bar[count] = '>';
        usleep(SPEED);
    }
    printf("\n");
}*/
